package eg.com.itiga.repositoryimpl.base;

// Generated Apr 9, 2010 5:21:53 PM by Hibernate Tools 3.2.2.GA

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import eg.com.itiga.domain.Branch;
import eg.com.itiga.domain.Department;
import eg.com.itiga.domain.RegCompany;
import eg.com.itiga.repository.base.BaseDao;
import eg.com.itiga.repository.exception.DataException;

/**
 * 
 * @author Tarek
 */

public class BaseDaoImpl implements BaseDao {

	public void saveOrUpdate(Object obj) throws DataException {
		Session session = HibernateUtil.getCurrentSession();
		try {
			session.saveOrUpdate(obj);			
			
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataException("Data Exception");
		}
	}

	public void delete(Object obj) throws DataException {
		Session session = HibernateUtil.getCurrentSession();
		try {
			session.delete(obj);
		} catch (HibernateException e) {
			throw new DataException("Data Exception");
		}

	}
	
	@Override
	public Object find(Class clazz, Integer id) throws DataException {
		System.out.println("find by id "+clazz.getName());
		Session session = HibernateUtil.getCurrentSession();		
		try {
			Object object=session.get(clazz, id);
			return object;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataException("Data Exception");
		}
	}
	
	

	public List findAll(Class clazz) throws DataException {
		System.out.println("class name "+clazz.getName());
		Session session = HibernateUtil.getCurrentSession();		
		try {
			System.out.println("good state");
			List l=null;
			Query query=session.createQuery("from "+clazz.getName());
			l=query.list();
			return l;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DataException("Data Exception");
		}
		
	}

	public void beginTransaction() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.beginTransaction();
	}

	public void commit() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.getTransaction().commit();
	}

	public void rollback() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.getTransaction().rollback();
	}

}
